
from rest_framework import serializers
from .models import *

class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = '__all__'
        read_only_fields = ['user']

class SubCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = SubCategory
        fields = '__all__'

class ColorSerializer(serializers.ModelSerializer):
    class Meta:
        model = Color
        fields = '__all__'
        read_only_fields = ['user']

class SeasonSerializer(serializers.ModelSerializer):
    class Meta:
        model = Season
        fields = '__all__'

class ThicknessSerializer(serializers.ModelSerializer):
    class Meta:
        model = Thickness
        fields = '__all__'
        read_only_fields = ['user']

class StorageLocationSerializer(serializers.ModelSerializer):
    class Meta:
        model = StorageLocation
        fields = '__all__'
        read_only_fields = ['user']

class BrandSerializer(serializers.ModelSerializer):
    class Meta:
        model = Brand
        fields = '__all__'
        read_only_fields = ['user']

class SizeTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = SizeType
        fields = '__all__'
        read_only_fields = ['user']

class SizeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Size
        fields = '__all__'
        # read_only_fields = ['user']

class MaterialSerializer(serializers.ModelSerializer):
    class Meta:
        model = Material
        fields = ['id', 'name']
        read_only_fields = ['user']

class TagTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = TagType
        fields = '__all__'
        read_only_fields = ['user']

class TagSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tag
        fields = '__all__'

class ImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Image
        fields = '__all__'

# class ClothesSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = Clothes
#         fields = '__all__'
#         read_only_fields = ['user']

class ClothesMaterialSerializer(serializers.ModelSerializer):
    material = MaterialSerializer(read_only=True)
    class Meta:
        model = ClothesMaterial
        fields = '__all__'
    def to_representation(self, instance):
        representation = super().to_representation(instance)
        material = representation.pop('material')
        representation['material_id'] = material['id']
        representation['material_name'] = material['name']
        return representation

class SuitsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Suits
        fields = '__all__'
        read_only_fields = ['user']

class LocationInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = LocationInfo
        fields = '__all__'

class WeatherConditionSerializer(serializers.ModelSerializer):
    class Meta:
        model = WeatherCondition
        fields = '__all__'

class FeelingSerializer(serializers.ModelSerializer):
    class Meta:
        model = Feeling
        fields = '__all__'

class RecordSerializer(serializers.ModelSerializer):
    class Meta:
        model = Record
        fields = '__all__'
        read_only_fields = ['user']  # 确保 user 字段是只读的

class ClothesSerializer(serializers.ModelSerializer):
    category_name = serializers.SerializerMethodField()
    brand_name = serializers.CharField(source='brand.name', read_only=True)
    season_names = serializers.SerializerMethodField()
    color_names = serializers.SerializerMethodField()
    storage_location_name = serializers.CharField(source='storage_location.name', read_only=True)
    size_names = serializers.SerializerMethodField()
    material_names = serializers.SerializerMethodField()
    tag_names = serializers.SerializerMethodField()
    thickness_name = serializers.CharField(source='thickness.name', read_only=True)
    materials = ClothesMaterialSerializer(source='clothesmaterial_set', many=True, read_only=True)
    
    records = serializers.SerializerMethodField()

    class Meta:
        read_only_fields = ['user']
        model = Clothes
        fields = '__all__'

    def get_category_name(self, obj):
        if obj.sub_category and obj.sub_category.parent:
            return f"{obj.sub_category.parent.name}-{obj.sub_category.name}"
        return None

    def get_season_names(self, obj):
        return [season.name for season in obj.seasons.all()]

    def get_color_names(self, obj):
        return [color.name for color in obj.colors.all()]

    def get_size_names(self, obj):
        return [size.name for size in obj.sizes.all()]

    def get_material_names(self, obj):
        return [material.name for material in obj.materials.all()]

    def get_tag_names(self, obj):
        return [tag.name for tag in obj.tags.all()]

    def get_records(self, obj):
        records = obj.records.all()
        sorted_records = sorted(records, key=lambda x: x.date)
        return [record.date for record in sorted_records]